//
//  MainScene.swift
//  G001
//
//  Created by remy on 2023/1/12.
//

struct PhysicsCategories: OptionSet {
    let rawValue: UInt32
    static let player = PhysicsCategories(rawValue: 1 << 0)
    static let enemy = PhysicsCategories(rawValue: 1 << 1)
    static let block = PhysicsCategories(rawValue: 1 << 2)
}

enum ZPositionLevel: CGFloat {
    case block = 10
    case moving = 20
    case player = 30
}

class MainScene: SKScene {
    
    var bounds: SKNode!
    var playerNode: PlayerNode!
    var isTouchPlayer: Bool = false
    var touchPosition: CGPoint = .zero
    var offsetTouch: CGPoint = .zero
    var limitMinPoint: CGPoint!
    var limitMaxPoint: CGPoint!
    
    override init(size: CGSize) {
        super.init(size: size)
        scaleMode = .aspectFill
        backgroundColor = FlatColor.midnightBlue
        physicsWorld.gravity = CGVector(dx: 0, dy: -3.0)
        physicsWorld.contactDelegate = self
        // 初始化
        limitMinPoint = CGPoint(x: PlayerNode.NODE_SIZE.width / 2, y: PlayerNode.NODE_SIZE.height / 2)
        limitMaxPoint = CGPoint(x: size.width - limitMinPoint.x, y: size.height - limitMinPoint.y)
        if Device.isAllScreen {
            limitMinPoint.y += 34
            limitMaxPoint.y -= 44
        }
        
        bounds = SKNode()
        let physicsBody = SKPhysicsBody(edgeLoopFrom: self.frame)
        physicsBody.restitution = 0.4
        bounds.physicsBody = physicsBody
        self.addChild(bounds)
        
        playerNode = PlayerNode()
        playerNode.position = CGPoint(x: size.width * 0.5, y: 180)
        self.addChild(playerNode)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func didMove(to view: SKView) {
        let node = FlowNode()
        node.position = CGPoint(x: size.width * 0.5, y: 480)
        self.addChild(node)
        
        
        
    }
    
    override func update(_ currentTime: TimeInterval) {
        if isTouchPlayer {
            let x = max(limitMinPoint.x, min(limitMaxPoint.x, touchPosition.x))
            let y = max(limitMinPoint.y, min(limitMaxPoint.y, touchPosition.y))
            playerNode.position = CGPoint(x: x, y: y)
//            print(touchPosition)
        }
    }
    
}

extension MainScene {
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        isTouchPlayer = false
        guard let touch = touches.first else { return }
        let position = touch.location(in: self)
        let node = self.atPoint(position)
        if node.name == PlayerNode.NODE_NAME {
            isTouchPlayer = true
            let x = position.x - playerNode.position.x
            let y = position.y - playerNode.position.y
            offsetTouch = CGPoint(x: x, y: y)
            touchPosition = playerNode.position
        }
    }
    
    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        guard isTouchPlayer else { return }
        guard let touch = touches.first else { return }
        let position = touch.location(in: self)
        touchPosition = CGPoint(x: position.x - offsetTouch.x, y: position.y - offsetTouch.y)
    }
    
    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        isTouchPlayer = false
    }
}

extension MainScene: SKPhysicsContactDelegate {
    
    func didBegin(_ contact: SKPhysicsContact) {
        
    }
}
